<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Type Erasure, Separate Compilation and Virtual Functions</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../boost_asio.html" title="Boost.Asio">
<link rel="up" href="../composition.html" title="Composition and Completion Tokens">
<link rel="prev" href="token_adapters.html" title="Completion Token Adapters">
<link rel="next" href="immediate_completion.html" title="Customising Immediate Completion">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="token_adapters.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../composition.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="immediate_completion.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="boost_asio.overview.composition.type_erasure"></a><a class="link" href="type_erasure.html" title="Type Erasure, Separate Compilation and Virtual Functions">Type
        Erasure, Separate Compilation and Virtual Functions</a>
</h4></div></div></div>
<p>
          The <code class="computeroutput"><span class="identifier">any_completion_handler</span><span class="special">&lt;&gt;</span></code> class template can be used to type-erase
          completion handlers. This template stores a completion handler in a runtime-polymorphic
          wrapper, and forwards the function call operator, associated executor,
          associated allocator, and associated cancellation slot to the target handler.
        </p>
<p>
          One use case is to enable separate compilation of asynchronous operation
          implementations. For example, we can declare our implementation function
          in a header file, and provide a thin asynchronous operation wrapper:
        </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">async_sleep_impl</span><span class="special">(</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">any_completion_handler</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">)&gt;</span> <span class="identifier">handler</span><span class="special">,</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">any_io_executor</span> <span class="identifier">ex</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">nanoseconds</span> <span class="identifier">duration</span><span class="special">);</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">CompletionToken</span><span class="special">&gt;</span>
<span class="keyword">inline</span> <span class="keyword">auto</span> <span class="identifier">async_sleep</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">any_io_executor</span> <span class="identifier">ex</span><span class="special">,</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">nanoseconds</span> <span class="identifier">duration</span><span class="special">,</span> <span class="identifier">CompletionToken</span><span class="special">&amp;&amp;</span> <span class="identifier">token</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">async_initiate</span><span class="special">&lt;</span>
    <span class="identifier">CompletionToken</span><span class="special">,</span> <span class="keyword">void</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">)&gt;(</span>
      <span class="identifier">async_sleep_impl</span><span class="special">,</span> <span class="identifier">token</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">ex</span><span class="special">),</span> <span class="identifier">duration</span><span class="special">);</span>
<span class="special">}</span>
</pre>
<p>
          By wrapping a call to <code class="computeroutput"><span class="identifier">async_initiate</span></code>,
          the <code class="computeroutput"><span class="identifier">async_sleep</span></code> template
          function adds full support for completion tokens. The definition of <code class="computeroutput"><span class="identifier">async_sleep_impl</span></code> is then put into a separately
          compiled source file:
        </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">async_sleep_impl</span><span class="special">(</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">any_completion_handler</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">)&gt;</span> <span class="identifier">handler</span><span class="special">,</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">any_io_executor</span> <span class="identifier">ex</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">chrono</span><span class="special">::</span><span class="identifier">nanoseconds</span> <span class="identifier">duration</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">auto</span> <span class="identifier">timer</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_shared</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">steady_timer</span><span class="special">&gt;(</span><span class="identifier">ex</span><span class="special">,</span> <span class="identifier">duration</span><span class="special">);</span>
  <span class="identifier">timer</span><span class="special">-&gt;</span><span class="identifier">async_wait</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">consign</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">handler</span><span class="special">),</span> <span class="identifier">timer</span><span class="special">));</span>
<span class="special">}</span>
</pre>
<p>
          Another use for <code class="computeroutput"><span class="identifier">any_completion_handler</span><span class="special">&lt;&gt;</span></code> is to vary the implementation behind
          an asynchronous operation at runtime, by using virtual functions:
        </p>
<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">line_reader</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
  <span class="comment">// ...</span>

  <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">CompletionToken</span><span class="special">&gt;</span>
  <span class="keyword">auto</span> <span class="identifier">async_read_line</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">prompt</span><span class="special">,</span> <span class="identifier">CompletionToken</span><span class="special">&amp;&amp;</span> <span class="identifier">token</span><span class="special">)</span>
  <span class="special">{</span>
    <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">async_initiate</span><span class="special">&lt;</span>
      <span class="identifier">CompletionToken</span><span class="special">,</span> <span class="keyword">void</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)&gt;(</span>
        <span class="special">[](</span><span class="keyword">auto</span> <span class="identifier">handler</span><span class="special">,</span> <span class="identifier">line_reader</span><span class="special">*</span> <span class="identifier">self</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">prompt</span><span class="special">)</span>
        <span class="special">{</span>
          <span class="identifier">self</span><span class="special">-&gt;</span><span class="identifier">async_read_line_impl</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">prompt</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">handler</span><span class="special">));</span>
        <span class="special">},</span> <span class="identifier">token</span><span class="special">,</span> <span class="keyword">this</span><span class="special">,</span> <span class="identifier">prompt</span><span class="special">);</span>
  <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
  <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">async_read_line_impl</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">prompt</span><span class="special">,</span>
      <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">any_completion_handler</span><span class="special">&lt;</span>
        <span class="keyword">void</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)&gt;</span> <span class="identifier">handler</span><span class="special">)</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
<span class="special">};</span>
</pre>
<p>
          A derived class provides the implementation of the operation:
        </p>
<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">stdin_line_reader</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">line_reader</span>
<span class="special">{</span>
<span class="keyword">private</span><span class="special">:</span>
  <span class="comment">// ...</span>

  <span class="keyword">void</span> <span class="identifier">async_read_line_impl</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">prompt</span><span class="special">,</span>
      <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">any_completion_handler</span><span class="special">&lt;</span>
        <span class="keyword">void</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)&gt;</span> <span class="identifier">handler</span><span class="special">)</span> <span class="identifier">override</span>
  <span class="special">{</span>
    <span class="comment">// ...</span>
  <span class="special">}</span>
<span class="special">};</span>
</pre>
<h6>
<a name="boost_asio.overview.composition.type_erasure.h0"></a>
          <span class="phrase"><a name="boost_asio.overview.composition.type_erasure.see_also"></a></span><a class="link" href="type_erasure.html#boost_asio.overview.composition.type_erasure.see_also">See Also</a>
        </h6>
<p>
          <a class="link" href="../../reference/any_completion_handler.html" title="any_completion_handler">any_completion_handler</a>,
          <a class="link" href="../../examples/cpp11_examples.html#boost_asio.examples.cpp11_examples.type_erasure">type erasure
          examples (C++11)</a>, <a class="link" href="../../examples/cpp20_examples.html#boost_asio.examples.cpp20_examples.type_erasure">type
          erasure examples (C++20)</a>.
        </p>
</div>
<div class="copyright-footer">Copyright © 2003-2025 Christopher M.
      Kohlhoff<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="token_adapters.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../composition.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="immediate_completion.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
